﻿using System;
using System.Linq;
using System.Web.Mvc;
using BLL;
using Model;
using Web.Helper;

namespace Web.Areas.Manager.Controllers
{


    [UserAuthorize]
    public partial class Sys_RoleController : Controller
    {
        Sys_Role_BLL _Sys_Role_BLL = new Sys_Role_BLL();
        Sys_Actions_BLL _Sys_Actions_BLL = new Sys_Actions_BLL();
        Sys_Controllers_BLL _Sys_Controllers_BLL = new Sys_Controllers_BLL();
        Sys_ActionsInControllersInRoles_BLL _Sys_ActionsInControllersInRoles_BLL = new Sys_ActionsInControllersInRoles_BLL();
        Sys_Users_BLL _Sys_Users_BLL = new Sys_Users_BLL();

        public virtual ActionResult Index(int? pageIndex, string keyword)
        {
            var model = _Sys_Role_BLL.Get();

            //筛选该用户所在企业的权限和公用的权限
            model = model.Where(a => a.Global || a.Sys_Enterprise.Sys_Users.Any(b=>b.Id.Equals(new Guid(User.Identity.Name))));

            if (!string.IsNullOrEmpty(keyword))
            {
                model = model.Where(a => a.RoleName.Contains(keyword));
            }

            return View(model.ToPagedList(pageIndex));
        }

        public virtual ActionResult Details(Guid id)
        {
            var item = _Sys_Role_BLL.Get(id);
            return View(item);
        }

        public virtual ActionResult Edit(Guid? id)
        {
            ViewData.Add("Sys_Actions", _Sys_Actions_BLL.Get());
            ViewData.Add("Sys_Controllers", _Sys_Controllers_BLL.Get().Where(a => a.Enable && !a.Global));

            var item = _Sys_Role_BLL.Get(id);
            return View(item);
        }

        [HttpPost]
        public virtual ActionResult Edit(Guid? id, Sys_Role collection)
        {

            if (!ModelState.IsValid)
            {
                ViewData.Add("Sys_Actions", _Sys_Actions_BLL.Get());
                ViewData.Add("Sys_Controllers", _Sys_Controllers_BLL.Get().Where(a => a.Enable && !a.Global));
                return View(collection);
            }


            Guid EnterpriseId = _Sys_Users_BLL.Get(new Guid(User.Identity.Name)).EnterpriseId;
            collection.EnterpriseId = EnterpriseId;

            _Sys_Role_BLL.Save(id, ref collection);

            foreach (var sys_controller in _Sys_Controllers_BLL.Get().Where(a => a.Enable && !a.Global))
            {
                foreach (var sys_action in _Sys_Actions_BLL.Get())
                {
                    _Sys_ActionsInControllersInRoles_BLL.Delete(sys_action.Id, sys_controller.Id, collection.Id);

                    if (Request[sys_controller.Id.ToString() + sys_action.Id.ToString()] != "false")
                    {
                        _Sys_ActionsInControllersInRoles_BLL.Insert(sys_action.Id, sys_controller.Id, collection.Id);
                        //   collection.Sys_ActionsInControllersInRoles.Add(new Sys_ActionsInControllersInRoles() { ActionId = sys_action.Id, ControllerId = sys_controller.Id, RoleId = collection.Id });
                    }
                }
            }
            return RedirectToAction("Index", MyrouteValues.Get());

        }

        [HttpDelete]
        public virtual ActionResult Delete(Guid Id)
        {
            _Sys_Role_BLL.DeleteObject(Id);
            return RedirectToAction("Index", MyrouteValues.Get());
        }
    }
}
